/*
 * 菜单管理 2010-01-15
 * author : tss
 * 
 */

//url 路径前缀
var startUrl = "/permission/menu/";
var Menu = {

	id : "Menu",
	enterNewUrl : startUrl + "enterNew.html",
	enterEditUrl : startUrl + "enterEdit.html",
	enterViewUrl : startUrl + "enterView.html",
	saveUrl : startUrl + "save.html",
	updateUrl : startUrl + "update.html",
	removeUrl : startUrl + "delete.html",
	queryUrl : startUrl + "list.html",
	backQueryUrl : startUrl + "toList.html",
	chkIds : "chkIds",
	form : function() {
		return document.forms[0];
	},

	// 校验
	validate : function() {
		/*
		 * try { return Validator.Validate(Menu.form(), 3); } catch (e) {
		 * alert(e.message); } return false;
		 */
		return true;
	},

	// 首页
	desktop : function() {

		doSubmit(Menu.form(), initVar.desktopUrl);
	},

	// 查询
	search : function search() {
		doSubmit(Menu.form(), Menu.queryUrl);
	},

	// 进入新增
	enterNew : function() {

		var id = getSelectFirstId(Menu.chkIds);
		doSubmit(Menu.form(), Menu.enterNewUrl + '?id=' + id);
	},

	// 进入修改
	enterEdit : function(id) {

		doSubmit(Menu.form(), Menu.enterEditUrl + '?id=' + id);
	},

	// 进入详细
	enterView : function(id) {

		doSubmit(Menu.form(), Menu.enterViewUrl + "?id=" + id);
	},

	// 删除
	remove : function() {

		commonRemove(Menu.form(), Menu.removeUrl, Menu.chkIds);
	},

	// 保存
	save : function() {

		doSubmit(Menu.form(), Menu.saveUrl);
	},

	// 修改
	update : function() {

		doSubmit(Menu.form(), Menu.updateUrl);
	},

	// 返回
	goBack : function() {

		doSubmit(Menu.form(), Menu.backQueryUrl);
	}
};

var name; // 查询菜单名称

function resInit() {

	rescService.getRescList(name, function(data) {
		initResTree(data);
	});
}

function initResTree(data) {

	d = new dTree('d','resTreeId',initVar.basePath+'/wzResource/common/js/dtree/');

	d.add(0, -1, '资源', '');
	if (data == null || data.length == 0) {
		return;
	}
	for ( var i = 0; i < data.length; i++) {

		var entity = data[i];
		var id = entity.id;
		var parentId = entity.parent.id;
		var name = entity.name;
		var url = entity.res;
		d.add(id, parentId, name, 'getName(' + id + ',\''+url+'\')');

		var childList = entity.childList;
		if (childList == null || childList.length == 0) {
			continue;
		}
		for ( var j = 0; j < childList.length; j++) {

			entity = childList[j];
			var id = entity.id;
			var parentId = entity.parent.id;
			var name = entity.name;
			var url = entity.res;
			d.add(id, parentId, name, 'getName(' + id + ',\''+url+'\')');

			var threeChildList = entity.childList;
			if (threeChildList == null || threeChildList.length == 0) {
				continue;
			}
			for ( var z = 0; z < threeChildList.length; z++) {

				entity = threeChildList[z];
				var id = entity.id;
				var parentId = entity.parent.id;
				var name = entity.name;
				var url = entity.res;
				d.add(id, parentId, name, 'getName(' + id + ',\''+url+'\')');
			}
		}
	}

}

function getName(id,name){
	
	$("#resId").val(id);
	$("#resName").val(name);
	close();
}

function close(){
	
	$("#resTreeId").html("");
	canClose = true;
}

var canClose = true;

// 加载
window.onload = function() {

	setTbStyle();
	execEvent(Menu, "INPUT", "button");
	execEvent(Menu, "A");

	resInit();
	
	
	$("#resName").bind("focus", function() {
        $(".dtree").css("z-index",5);
		$("#resTreeId").html(d.toString());
	});
	$("#resName").bind("blur", function() {
		if(canClose){
			close();
		}
	});
	$("#resTreeId").mouseover(function() {
	    canClose = false;
	}).mouseout( function() {
	    canClose = true;
	}).blur(function(){
		if(canClose){
			close();
		}
	}); 
	

};
